package com.chen.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.entity.vo.MateVo;
import com.chen.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.Result;
import com.chen.service.IMateService;
import com.chen.entity.Mate;

import org.springframework.web.bind.annotation.RestController;

import static com.chen.common.Constants.CODE_600;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Y·C
 * @since 2023-04-23
 */
@Api(tags = "红娘信息")
@RestController
@RequestMapping("/mate")
public class MateController {

    @Resource
    private IMateService mateService;


    @ApiOperation("添加红娘或者修改")
    @PostMapping
    public Result save(@RequestBody MateVo mateVo) {
        Mate mate = new Mate();

        //将mateVo信息复制给mate
        BeanUtil.copyProperties(mateVo, mate, true);
        mateService.saveOrUpdate(mate);
        return Result.success();
    }

    @ApiOperation("删除一条或者多条红娘信息")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        mateService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation("查询所有红娘信息")
    @GetMapping
    public Result findAll() {
        return Result.success(mateService.list());
    }


    @ApiOperation("分页查询红娘所有信息条件查询")
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "")  String name)  {
        QueryWrapper<Mate> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.like("name",name);
        return Result.success(mateService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("牵线接口 传红娘id")
    @GetMapping("/Strings")
    public Result Strings(@RequestParam Integer id){

        Mate mate = mateService.selectById(id);
        Integer mateNumber = mate.getMateNumber();
        mateNumber = mateNumber + 1;
        mate.setMateNumber(mateNumber);
        mateService.updateById(mate);
        String message = "您已经被红娘"+mate.getName()+"接洽"+",正在服务中端婚恋厕";
        return Result.success(message);
    }

    @ApiOperation("评价接口 '1'代表好评，'2'代表中评，'3'代表差评")
    @GetMapping("/appraise")
    public Result appraise(@RequestParam Integer appraiseId,
                           @RequestParam Integer id){

        Mate mate = mateService.selectById(id);
        Integer goodNumber = mate.getGoodNumber();
        Integer modNumber = mate.getModNumber();
        Integer badNumber = mate.getBadNumber();

        if (appraiseId==1){
            goodNumber = goodNumber + 1;
            mate.setGoodNumber(goodNumber);
        }else if (appraiseId==2){
           modNumber= modNumber + 1;
            mate.setModNumber(modNumber);
        }else if (appraiseId==3){
           badNumber = badNumber + 1;
            mate.setBadNumber(badNumber);
        }else {

            throw new ServiceException(CODE_600,"请评价正确的级别");
        }

        return Result.success(mateService.updateById(mate));
    }

}

